#include "FreeRTOS.h"        //RTOS主文件
#include "task.h"            //RTOS任务管理
#include "queue.h"           //RTOS队列管理
#include "semphr.h"          //RTOS消息管理
#include "event_groups.h"    //RTOS事件管理
#include "timers.h"          //RTOS定时管理

#include <string.h>
#include <stdio.h>
#include <ctype.h>

#include "../Tool/Cm_Backtrace/cm_backtrace.h"
#include "./Driver/Debug/debug_driver.h"
#include "./BSP/WWDG_BSP/wwdg_bsp.h"

#include "./APP/App_io_ctrl/app_io_ctrl.h"
//#include "./APP/App_button/app_button.h"
//#include "./APP/App_gui/app_oled.h"
#include "./APP/App_FatFS/app_fatfs.h"

//#include "./APP/App_ModBus/app_modbus.h"
#include "./APP/App_lvgl/app_lvgl.h"
#include "./APP/App_LwIP/app_lwip.h"


#define Hard_Version               "V1.0.0"
#define Soft_Version               "V0.3.0"


/* STM32 M3 M4中断优先级分组为4，即4bit都用来表示抢占优先级，范围为：0~15 */
static inline void Hardware_Init(void)
{
    /* 关闭JTAG的引脚默认功能 只开启SW的引脚默认功能 */
    WHT_GPIO_BSP.WHT_Set_Clock(PortA, ENABLE);
    WHT_GPIO_BSP.WHT_Set_Clock(PortB, ENABLE);
    WHT_GPIO_BSP.WHT_Set_Mode(PortA, Pin15, Mode_In_FLOATING);
    WHT_GPIO_BSP.WHT_Set_Mode(PortB, Pin3 | Pin4, Mode_In_FLOATING);
    WHT_GPIO_BSP.WHT_Set_Clock(PortA, DISABLE);
    WHT_GPIO_BSP.WHT_Set_Clock(PortB, DISABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    WHT_Debug_Driver.WHT_Init();
    WHT_WWDG_Config_t WWDG_Config = {36000, 10, 50, ENABLE};
    ////WHT_WWDG_BSP.WHT_Config(&WWDG_Config);
}



static void App_Start_Task(void* parameter);//启动其它任务的启动任务
static void Admin_Task(void* parameter);    //管理其它任务的管理任务
static void WHT_Test_Task(void* parameter); //测试任务

/**********************************************************************/
int main(void)
{
    Hardware_Init();//先初始化硬件
    xTaskCreate(App_Start_Task, "App_Start_Task", 256, NULL, configMAX_PRIORITIES, NULL);
    vTaskStartScheduler();
    while(1);
}
/**********************************************************************/
static void App_Start_Task(void *parameter)
{
    printf("FreeRTOS Version: %s\r\n", tskKERNEL_VERSION_NUMBER);
    cm_backtrace_init("WHT-STM32F429", Hard_Version, Soft_Version);
    printf("CmBacktrace Version: %s\r\n", CMB_SW_VERSION);
    cm_backtrace_firmware_info();
    printf("SystemCoreClock = %uMHz\r\n", SystemCoreClock / 1000000);
    for (uint8_t Start_Countdown_Time = 3; Start_Countdown_Time > 0; Start_Countdown_Time--)
    {
        printf("STM32F429 Start Countdown is %d Sec\r\n", Start_Countdown_Time - 1);
        vTaskDelay(1000 / portTICK_RATE_MS);
    }

    xTaskCreate(Admin_Task, "Admin_Task", 320, NULL, 1, NULL);
    xTaskCreate(WHT_Test_Task, "WHT_Test_Task", 256, NULL, 3, NULL);
    /*************添加其它任务代码**************/
    //WHT_Button_t WHT_Button;
    //WHT_Button_Init(5, &WHT_Button);
    //WHT_OLED_Install(3);
    //WHT_ModBus_Install(6);
    WHT_LVGL_Install(10);
    //WHT_FatFS_Install(9);
    //WHT_LWIP_Install(10);
    /*************添加其它任务代码**************/
    vTaskDelete(NULL);//删除启动任务
}
/**********************************************************************/


/*RTOS任务实时状态任务*/
static void Admin_Task(void* parameter)
{
    int count = 3;
    char Task_buf[512];
    
    for(;;)
    {
        vTaskList(Task_buf);
        WHT_Debug_Driver.WHT_Printf("Task_Name     State  Priority  Stack  Number\r\n", 46);//格局已调试OK
        WHT_Debug_Driver.WHT_Printf(Task_buf, strlen(Task_buf));
        WHT_Debug_Driver.WHT_Printf("\r\n", 2);
        vTaskDelay(1000 / portTICK_RATE_MS);
        if (--count == 0)
        {
            vTaskDelete(NULL);
        }
    }
}


static void timer1_hook(void* parameter)
{

}

/*调试任务*/
static void WHT_Test_Task(void* parameter)
{
    uint8_t receive_buff[300];
    uint16_t Length;

    TimerHandle_t timer1_Handle =xTimerCreate("timer1_hook", 100, pdTRUE, NULL, timer1_hook);
    xTimerStart(timer1_Handle, 0);

    WHT_IO_Ctrl.WHT_Install(5);
    WHT_IO_Ctrl.WHT_Set_Flash(IO_Ctrl0, 0, 20, 1000, IO_Ctrl_OFF);

    for (; ;)
    {
        Length = WHT_Debug_Driver.WHT_Scanf(receive_buff, sizeof(receive_buff), portMAX_DELAY);
        printf("\r\nreceive %d\r\n", Length);
        WHT_Debug_Driver.WHT_Printf((char*)receive_buff, Length);//原样发出
        vTaskDelay(100);
        for (size_t i = 0; i < strlen("reset"); i++)
        {
            receive_buff[i] = tolower((char)receive_buff[i]);
        }
        if (strstr(receive_buff, "reset") != 0)
        {
            NVIC_SystemReset();
        }
    }
}
